package com.hzp.micromall.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzp.micromall.admin.dao.*;
import com.hzp.micromall.admin.entity.*;
import com.hzp.micromall.admin.service.UmsResourceService;
import com.hzp.micromall.admin.service.UmsRoleService;
import com.hzp.micromall.common.core.api.CommonPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author houzipeng
 * @Date 2022/2/24 23:06
 * @Version 1.0
 * 后台角色管理实现类
 */
@Service
public class UmsRoleServiceImpl implements UmsRoleService {
    @Autowired
    private UmsRoleDao roleDao;
    @Autowired
    private UmsRoleMenuRelationDao roleMenuRelationDao;
    @Autowired
    private UmsRoleResourceRelationDao roleResourceRelationDao;
    @Autowired
    private UmsResourceService resourceService;
    @Autowired
    private UmsMenuDao menuDao;
    @Autowired
    private UmsResourceDao resourceDao;

    @Override
    public int createRole(UmsRole role) {
        role.setCreateTime(new Date());
        role.setAdminCount(0);
        role.setSort(0);
        return roleDao.insert(role);
    }

    @Override
    public int updateRoleById(Long id, UmsRole role) {
        role.setId(id);
        return roleDao.updateById(role);
    }

    @Override
    public int deleteRoleBatchById(List<Long> ids) {
        int count = roleDao.deleteBatchIds(ids);
        resourceService.initResourceRolesMap();
        return count;
    }

    @Override
    public List<UmsRole> list() {
        return roleDao.selectList(null);
    }

    @Override
    public CommonPage<UmsRole> listPage(String keyword, Integer pageSize, Integer pageNum) {
        Page<UmsRole> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsRole> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.like(UmsRole::getName, keyword);
        }
        roleDao.selectPage(page, wrapper);
        return new CommonPage<UmsRole>(page.getCurrent(), page.getTotal(), page.getSize(), page.getRecords());
    }

    @Override
    public List<UmsMenu> getMenuListByRoleId(Long roleId) {
        return menuDao.getMenuListByRoleId(roleId);
    }

    @Override
    public List<UmsResource> getResourceListByRoleId(Long roleId) {
        return resourceDao.getResourceListByRoleId(roleId);
    }

    @Override
    public int allocMenu(Long roleId, List<Long> menuIds) {
        //先删除原有关系
        LambdaQueryWrapper<UmsRoleMenuRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsRoleMenuRelation::getRoleId, roleId);
        roleMenuRelationDao.delete(wrapper);
        //批量插入新关系
        for (Long menuId : menuIds) {
            UmsRoleMenuRelation relation = new UmsRoleMenuRelation();
            relation.setRoleId(roleId);
            relation.setMenuId(menuId);
            roleMenuRelationDao.insert(relation);
        }
        return menuIds.size();
    }

    @Override
    public int allocResource(Long roleId, List<Long> resourceIds) {
        //先删除原有关系
        LambdaQueryWrapper<UmsRoleResourceRelation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsRoleResourceRelation::getRoleId, roleId);
        roleResourceRelationDao.delete(wrapper);
        //批量插入新关系
        for (Long resourceId : resourceIds) {
            UmsRoleResourceRelation relation = new UmsRoleResourceRelation();
            relation.setRoleId(roleId);
            relation.setResourceId(resourceId);
            roleResourceRelationDao.insert(relation);
        }
        resourceService.initResourceRolesMap();
        return resourceIds.size();
    }
}
